<?php

include 'include/db.php';

$filename = $google_folder . "export_" . (date("d_m_Y_H_i_s",time())) . ".txt";
$filename = $google_folder . "data_feed.txt";

$fp = fopen($filename, "w");

//$res = mysql_query("SELECT * FROM $table");
$res = mysql_query($sql);
$line = "id\tgtin\ttitle\tlink\tprice\tdescription\timage_link\tcondition\tgewicht\tmarke\tavailability\tGoogle product category\tproduct_type";
$line .= "\n";
fputs($fp, $line);

// remove the result pointer back to the start
mysql_data_seek($res, 0);

// and loop through the actual data
while($row = mysql_fetch_assoc($res)) {
    //prepare id
        $id = $row["products_model"];
        //check if we use EAN or ASIN (prefer EAN)
        if ($row["products_ean"]!="") {
                $product_id_type = "ean";
                $gtin = $product_id;
                $product_id = $row["products_ean"];
                $product_id = preg_replace('/\W/','',$product_id);
                $gtin = $product_id;
                $ean_chek_result = Validate_ISPN::ean13($product_id);
                if ($ean_chek_result != 1 ){
                        $mail_message = "the EAN [". $product_id . "] in article with sku: " . $id . " is not valid" ;
                        $mailer = Mail::factory('smtp',$mail_options);
                        if (true === PEAR::isError($mailer)) {
                           die ($mailer->getMessage());
                        }
                        $mail_res = $mailer->send($mail_receipient,$mail_headers,$mail_message);
                        if (true === PEAR::isError($mail_res)) {
                           die ($mail_res->getMessage());
                        }
                        //echo $mail_message . "\n";
                }
        }
        else {
                $product_id_type = "ASIN";
                $gtin = "";
                $product_id = $row["products_ASIN"];
        }
        $link = $osc_products_url . $row["products_id"];
        //prepare products_name
        $title = $row["products_name"];

        //remove "\t" and "\r\n" from description
        $string = $row["products_description"];
        $string = str_replace("\t",'',$string);
        $string = str_replace("\r\n",'',$string);
        $products_description = $string;

        //errechne VK_Preis und runde auf 2 Nachkommastellen
        $price = round(($row["products_price"] * $tax), 2);


        //build Image URL for Amazon
        $product_image = $osc_images . $row["products_image"];

        $condition = "neu";
        //check $row[3] => Hersteller
        if ($row["manufacturers_name"] == "")
                {
                $manufacturer = "EWH";
                }
        else $manufacturer = $row["manufacturers_name"];

 		//set product_weight
        $product_weight = $row["products_weight"];

        //set availability for product
        $availability = "in stock";
        $product_type = "Bauteile > Bauzubehör";
        $google_product_type = $product_type;

        //build item line
        $line = $product_id . "\t" .
                $gtin . "\t" .
                $title . "\t" .
                $link . "\t" .
                $price . "\t" .
                $products_description . "\t" .
                $product_image . "\t" .
                $condition . "\t" .
                $product_weight . "\t" .
                $manufacturer . "\t" .
                $availability . "\t" .
                $google_product_type . "\t" .
                $product_type
                ;

    $line .= "\n";
    $line = convertToWindowsCharset($line);
    fputs($fp, $line);

}

fclose($fp);
?>